package cn.dao;

import cn.pojo.Admin;
import cn.util.DaoUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author jia
 * @date 2019-03-21-9:37
 */
public class AdminDao {

    /**
     * 根据用户名和密码
     * 查询管理员用户
     */
    public Admin getAdmin(String name, String password) {
        Admin admin = null;
        String sql = "select * from admin WHERE binary adminName = ? AND binary adminPassword = ?";
        try (Connection connection = DaoUtil.getConnection();
             PreparedStatement ps = connection.prepareStatement(sql)) {

            ps.setString(1, name);
            ps.setString(2, password);

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                admin = new Admin();
                admin.setAdminId(rs.getInt("adminId"));
                admin.setAdminName(rs.getString("adminName"));
                admin.setAdminPassword(rs.getString("adminPassword"));
                admin.setStatus(rs.getInt("status"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return admin;
    }
    /**
     * 修改管理员
     */
    public void update(Admin admin) {
        String sql = "update admin set adminName = ?,adminPassword = ? where adminId = ?";
        try (Connection connection = DaoUtil.getConnection();
             PreparedStatement ps = connection.prepareStatement(sql)) {

            ps.setString(1, admin.getAdminName());
            ps.setString(2, admin.getAdminPassword());
            ps.setInt(3, admin.getAdminId());

            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
